﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using Assist;
using DBService;
using LampUseFactorManager;
using Microsoft.Win32;

namespace YJKFamilyLibDataManager
{
	public abstract class FamilyLibDatabase : Database
	{
		public string Name
		{
			get
			{
				return this.mName;
			}
		}

		public static List<FamilyLibDatabase> GetUserDbs(string path, string version)
		{
			List<FamilyLibDatabase> list = new List<FamilyLibDatabase>();
			FamilyLibDatabase sqliteDB = FamilyLibDatabase.GetSqliteDB(path, SubjectType.Architecture, version);
			if (sqliteDB != null)
			{
				list.Add(sqliteDB);
			}
			FamilyLibDatabase sqliteDB2 = FamilyLibDatabase.GetSqliteDB(path, SubjectType.Electric, version);
			if (sqliteDB2 != null)
			{
				list.Add(sqliteDB2);
			}
			FamilyLibDatabase sqliteDB3 = FamilyLibDatabase.GetSqliteDB(path, SubjectType.HVAC, version);
			if (sqliteDB3 != null)
			{
				list.Add(sqliteDB3);
			}
			FamilyLibDatabase sqliteDB4 = FamilyLibDatabase.GetSqliteDB(path, SubjectType.Plumbing, version);
			if (sqliteDB4 != null)
			{
				list.Add(sqliteDB4);
			}
			FamilyLibDatabase sqliteDB5 = FamilyLibDatabase.GetSqliteDB(path, SubjectType.System, version);
			if (sqliteDB5 != null)
			{
				list.Add(sqliteDB5);
			}
			return list;
		}

		public static List<FamilyLibDatabase> GetLocalSystemDb(string version)
		{
			return new List<FamilyLibDatabase>
			{
				FamilyLibDatabase.GetSqliteDB(SubjectType.System, version)
			};
		}

		public static List<FamilyLibDatabase> GetLocalDbs(string version)
		{
			return new List<FamilyLibDatabase>
			{
				FamilyLibDatabase.GetSqliteDB(SubjectType.Architecture, version),
				FamilyLibDatabase.GetSqliteDB(SubjectType.Electric, version),
				FamilyLibDatabase.GetSqliteDB(SubjectType.HVAC, version),
				FamilyLibDatabase.GetSqliteDB(SubjectType.Plumbing, version),
				FamilyLibDatabase.GetSqliteDB(SubjectType.System, version)
			};
		}

		public static FamilyLibDatabase GetSqlServerProjectDb(string configFileName, string dbName)
		{
			string fileName = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";
			string dataFromConfigFile = XmlUtils.GetDataFromConfigFile(fileName, "Server", "value");
			string dataFromConfigFile2 = XmlUtils.GetDataFromConfigFile(fileName, "Uid", "value");
			string dataFromConfigFile3 = XmlUtils.GetDataFromConfigFile(fileName, "Pwd", "value");
			ConnectData cd = new ConnectData(dataFromConfigFile, dataFromConfigFile2, dataFromConfigFile3);
			return FamilyLibDatabase.GetSqlServerDB(dbName, cd);
		}

		public static FamilyLibDatabase GetMySqlProjectDb(string configFileName, string dbName)
		{
			string fileName = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";
			string dataFromConfigFile = XmlUtils.GetDataFromConfigFile(fileName, "Server", "value");
			string dataFromConfigFile2 = XmlUtils.GetDataFromConfigFile(fileName, "Uid", "value");
			string dataFromConfigFile3 = XmlUtils.GetDataFromConfigFile(fileName, "Pwd", "value");
			ConnectData cd = new ConnectData(dataFromConfigFile, dataFromConfigFile2, dataFromConfigFile3);
			return FamilyLibDatabase.GetMySqlDB(dbName, cd);
		}

		public static List<FamilyLibDatabase> GetDbs(string version)
		{
			List<FamilyLibDatabase> list = new List<FamilyLibDatabase>();
			try
			{
				DbData dbData = ConfigUtils.ReadFamilyLibXmlFile("族库类型", version);
				if (dbData is LocalDbData)
				{
					if (FamilyLibDatabase.GetSqliteDB(SubjectType.Architecture, version) != null)
					{
						list.Add(FamilyLibDatabase.GetSqliteDB(SubjectType.Architecture, version));
					}
					if (FamilyLibDatabase.GetSqliteDB(SubjectType.Electric, version) != null)
					{
						list.Add(FamilyLibDatabase.GetSqliteDB(SubjectType.Electric, version));
					}
					if (FamilyLibDatabase.GetSqliteDB(SubjectType.HVAC, version) != null)
					{
						list.Add(FamilyLibDatabase.GetSqliteDB(SubjectType.HVAC, version));
					}
					if (FamilyLibDatabase.GetSqliteDB(SubjectType.Plumbing, version) != null)
					{
						list.Add(FamilyLibDatabase.GetSqliteDB(SubjectType.Plumbing, version));
					}
					if (FamilyLibDatabase.GetSqliteDB(SubjectType.System, version) != null)
					{
						list.Add(FamilyLibDatabase.GetSqliteDB(SubjectType.System, version));
					}
				}
				else if (dbData is UserDbData)
				{
					string databasePath = (dbData as UserDbData).DatabasePath;
					if (FamilyLibDatabase.GetSqliteDB(databasePath, SubjectType.Architecture, version) != null)
					{
						list.Add(FamilyLibDatabase.GetSqliteDB(databasePath, SubjectType.Architecture, version));
					}
					if (FamilyLibDatabase.GetSqliteDB(databasePath, SubjectType.Electric, version) != null)
					{
						list.Add(FamilyLibDatabase.GetSqliteDB(databasePath, SubjectType.Electric, version));
					}
					if (FamilyLibDatabase.GetSqliteDB(databasePath, SubjectType.HVAC, version) != null)
					{
						list.Add(FamilyLibDatabase.GetSqliteDB(databasePath, SubjectType.HVAC, version));
					}
					if (FamilyLibDatabase.GetSqliteDB(databasePath, SubjectType.Plumbing, version) != null)
					{
						list.Add(FamilyLibDatabase.GetSqliteDB(databasePath, SubjectType.Plumbing, version));
					}
					if (FamilyLibDatabase.GetSqliteDB(databasePath, SubjectType.System, version) != null)
					{
						list.Add(FamilyLibDatabase.GetSqliteDB(databasePath, SubjectType.System, version));
					}
				}
				else if (dbData is ServerDbData)
				{
					string fileName = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";
					string dataFromConfigFile = XmlUtils.GetDataFromConfigFile(fileName, "Server", "value");
					string dataFromConfigFile2 = XmlUtils.GetDataFromConfigFile(fileName, "Uid", "value");
					string dataFromConfigFile3 = XmlUtils.GetDataFromConfigFile(fileName, "Pwd", "value");
					ConnectData cd = new ConnectData(dataFromConfigFile, dataFromConfigFile2, dataFromConfigFile3);
					FamilyLibDatabase mySqlDB = FamilyLibDatabase.GetMySqlDB(SubjectType.HVAC, version, cd);
					FamilyLibDatabase mySqlDB2 = FamilyLibDatabase.GetMySqlDB(SubjectType.Architecture, version, cd);
					FamilyLibDatabase mySqlDB3 = FamilyLibDatabase.GetMySqlDB(SubjectType.Electric, version, cd);
					FamilyLibDatabase mySqlDB4 = FamilyLibDatabase.GetMySqlDB(SubjectType.Plumbing, version, cd);
					FamilyLibDatabase mySqlDB5 = FamilyLibDatabase.GetMySqlDB(SubjectType.System, version, cd);
					list.Add(mySqlDB2);
					list.Add(mySqlDB3);
					list.Add(mySqlDB);
					list.Add(mySqlDB4);
					list.Add(mySqlDB5);
				}
				else if (dbData is CompanyDbData)
				{
					string fileName2 = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";
					string dataFromConfigFile4 = XmlUtils.GetDataFromConfigFile(fileName2, "Server", "value");
					string dataFromConfigFile5 = XmlUtils.GetDataFromConfigFile(fileName2, "Uid", "value");
					string dataFromConfigFile6 = XmlUtils.GetDataFromConfigFile(fileName2, "Pwd", "value");
					ConnectData cd2 = new ConnectData(dataFromConfigFile4, dataFromConfigFile5, dataFromConfigFile6);
					FamilyLibDatabase mySqlDB6 = FamilyLibDatabase.GetMySqlDB(SubjectType.Company, version, cd2);
					list.Add(mySqlDB6);
				}
			}
			catch
			{
			}
			return list;
		}

		public static FamilyLibDatabase GetDb(string version, SubjectType st)
		{
			FamilyLibDatabase result = null;
			try
			{
				DbData dbData = ConfigUtils.ReadFamilyLibXmlFile("族库类型", version);
				if (dbData is LocalDbData)
				{
					result = FamilyLibDatabase.GetSqliteDB(st, version);
				}
				else if (dbData is UserDbData)
				{
					result = FamilyLibDatabase.GetSqliteDB(st, version);
				}
				else if (dbData is ServerDbData)
				{
					string fileName = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";
					string dataFromConfigFile = XmlUtils.GetDataFromConfigFile(fileName, "Server", "value");
					string dataFromConfigFile2 = XmlUtils.GetDataFromConfigFile(fileName, "Uid", "value");
					string dataFromConfigFile3 = XmlUtils.GetDataFromConfigFile(fileName, "Pwd", "value");
					ConnectData cd = new ConnectData(dataFromConfigFile, dataFromConfigFile2, dataFromConfigFile3);
					result = FamilyLibDatabase.GetSqlServerDB(st, version, cd);
				}
			}
			catch
			{
			}
			return result;
		}

		public static string GetDefFamilyLibPath()
		{
			string text = Assembly.GetExecutingAssembly().Location;
			int length = text.LastIndexOf("\\");
			text = text.Substring(0, length);
			int length2 = text.LastIndexOf("\\");
			text = text.Substring(0, length2);

            return @"C:\Hongye\HYArch2018\FamilyLib";

			return text + "\\FamilyLib";
		}

		public static string GetRegistData(string revitVer)
		{
			string text = "";
			string result;
			try
			{
				RegistryKey localMachine = Registry.LocalMachine;
				RegistryKey registryKey = localMachine.OpenSubKey("SOFTWARE", false);
				RegistryKey registryKey2 = registryKey.OpenSubKey("Hongye", false);
				RegistryKey registryKey3 = registryKey2.OpenSubKey("HYFamilyLib", false);
				double num = 1.0;
				string name = "1.0";
				foreach (string text2 in registryKey3.GetSubKeyNames())
				{
					if (Convert.ToDouble(text2) > num)
					{
						num = Convert.ToDouble(text2);
						name = text2;
					}
				}
				RegistryKey registryKey4 = registryKey3.OpenSubKey(name, false);
				RegistryKey registryKey5 = registryKey4.OpenSubKey(revitVer, false);
				text = registryKey5.GetValue("Location").ToString() + "FamilyLib";
				result = text;
			}
			catch
			{
				result = text;
			}
			return result;
		}

		public static SqliteDatabase GetSqliteDB(SubjectType sType, string version)
		{
			string text = FamilyUtil.PathIni.ReadPath(version);
			if (text == "")
			{
				text = FamilyLibDatabase.GetDefFamilyLibPath();
				if (text == "")
				{
					throw new Exception(text + "\n没有检测到本地族库，请安装族库。");
				}
			}
			switch (sType)
			{
			case SubjectType.Architecture:
			{
				FileInfo fileInfo = new FileInfo(text + "\\Architecture" + version + ".db");
				if (fileInfo.Exists)
				{
					return new SqliteDatabase(fileInfo.FullName);
				}
				throw new Exception("没有检测到本地族库，请安装族库。");
			}
			case SubjectType.HVAC:
			{
				FileInfo fileInfo2 = new FileInfo(text + "\\HVAC" + version + ".db");
				if (fileInfo2.Exists)
				{
					return new SqliteDatabase(fileInfo2.FullName);
				}
				throw new Exception("没有检测到本地族库，请安装族库。");
			}
			case SubjectType.Electric:
			{
				FileInfo fileInfo3 = new FileInfo(text + "\\Electric" + version + ".db");
				if (fileInfo3.Exists)
				{
					return new SqliteDatabase(fileInfo3.FullName);
				}
				throw new Exception("没有检测到本地族库，请安装族库。");
			}
			case SubjectType.Plumbing:
			{
				FileInfo fileInfo4 = new FileInfo(text + "\\Plumbing" + version + ".db");
				if (fileInfo4.Exists)
				{
					return new SqliteDatabase(fileInfo4.FullName);
				}
				throw new Exception("没有检测到本地族库，请安装族库。");
			}
			case SubjectType.System:
			{
				FileInfo fileInfo5 = new FileInfo(text + "\\System" + version + ".db");
				if (fileInfo5.Exists)
				{
					return new SqliteDatabase(fileInfo5.FullName);
				}
				throw new Exception("没有检测到本地族库，请安装族库。");
			}
			case SubjectType.Project:
			{
				FileInfo fileInfo6 = new FileInfo(text + "\\Project" + version + ".db");
				if (fileInfo6.Exists)
				{
					return new SqliteDatabase(fileInfo6.FullName);
				}
				throw new Exception("没有检测到本地族库，请安装族库。");
			}
			case SubjectType.MyCollect:
			{
				FileInfo fileInfo7 = new FileInfo(text + "\\MyCollect" + version + ".db");
				if (fileInfo7.Exists)
				{
					return new SqliteDatabase(fileInfo7.FullName);
				}
				throw new Exception("没有检测到本地族库，请安装族库。");
			}
			}
			return null;
		}

		public static SqliteDatabase GetSqliteDB(string fileName, string ver)
		{
			string text = FamilyUtil.PathIni.ReadPath(ver);
			if (text != "")
			{
				FileInfo fileInfo = new FileInfo(text + "\\" + fileName);
				if (fileInfo.Exists)
				{
					return new SqliteDatabase(fileInfo.FullName);
				}
				throw new Exception("没有检测到本地族库，请安装族库。");
			}
			else
			{
				FileInfo fileInfo2 = new FileInfo(FamilyLibDatabase.GetDefFamilyLibPath() + "\\" + fileName);
				if (!fileInfo2.Exists)
				{
					throw new Exception("没有检测到本地族库，请安装族库。");
				}
				return new SqliteDatabase(fileInfo2.FullName);
			}
		}

		public static SqliteDatabase GetSqliteDB(string path, SubjectType sType, string version)
		{
			switch (sType)
			{
			case SubjectType.Architecture:
			{
				if (!Directory.Exists(path))
				{
					Directory.CreateDirectory(path);
				}
				string fileName = path + "\\Architecture" + version + ".db";
				if (new FileInfo(fileName).Exists)
				{
					return new SqliteDatabase(path + "\\Architecture" + version + ".db");
				}
				byte[] pReadByte = Resource1.ResourceManager.GetObject("Architecture") as byte[];
				FileInfo fileInfo = new FileInfo(fileName);
				if (!fileInfo.Exists)
				{
                    Assist.FileUtil.WriteFile(pReadByte, fileName);
				}
				return new SqliteDatabase(fileName);
			}
			case SubjectType.HVAC:
			{
				if (!Directory.Exists(path))
				{
					Directory.CreateDirectory(path);
				}
				string fileName2 = path + "\\HVAC" + version + ".db";
				if (new FileInfo(fileName2).Exists)
				{
					return new SqliteDatabase(path + "\\HVAC" + version + ".db");
				}
				byte[] pReadByte2 = Resource1.ResourceManager.GetObject("HVAC") as byte[];
				FileInfo fileInfo2 = new FileInfo(fileName2);
				if (!fileInfo2.Exists)
				{
                    Assist.FileUtil.WriteFile(pReadByte2, fileName2);
				}
				return new SqliteDatabase(fileName2);
			}
			case SubjectType.Electric:
			{
				if (!Directory.Exists(path))
				{
					Directory.CreateDirectory(path);
				}
				string fileName3 = path + "\\Electric" + version + ".db";
				if (new FileInfo(fileName3).Exists)
				{
					return new SqliteDatabase(path + "\\Electric" + version + ".db");
				}
				byte[] pReadByte3 = Resource1.ResourceManager.GetObject("Electric") as byte[];
				FileInfo fileInfo3 = new FileInfo(fileName3);
				if (!fileInfo3.Exists)
				{
                    Assist.FileUtil.WriteFile(pReadByte3, fileName3);
				}
				return new SqliteDatabase(fileName3);
			}
			case SubjectType.Plumbing:
			{
				if (!Directory.Exists(path))
				{
					Directory.CreateDirectory(path);
				}
				string fileName4 = path + "\\Plumbing" + version + ".db";
				if (new FileInfo(fileName4).Exists)
				{
					return new SqliteDatabase(path + "\\Plumbing" + version + ".db");
				}
				byte[] pReadByte4 = Resource1.ResourceManager.GetObject("Plumbing") as byte[];
				FileInfo fileInfo4 = new FileInfo(fileName4);
				if (!fileInfo4.Exists)
				{
                    Assist.FileUtil.WriteFile(pReadByte4, fileName4);
				}
				return new SqliteDatabase(fileName4);
			}
			case SubjectType.System:
			{
				if (!Directory.Exists(path))
				{
					Directory.CreateDirectory(path);
				}
				string fileName5 = path + "\\System" + version + ".db";
				if (new FileInfo(fileName5).Exists)
				{
					return new SqliteDatabase(path + "\\System" + version + ".db");
				}
				byte[] pReadByte5 = Resource1.ResourceManager.GetObject("System") as byte[];
				FileInfo fileInfo5 = new FileInfo(fileName5);
				if (!fileInfo5.Exists)
				{
                    Assist.FileUtil.WriteFile(pReadByte5, fileName5);
				}
				return new SqliteDatabase(fileName5);
			}
			case SubjectType.Project:
			{
				if (!Directory.Exists(path))
				{
					Directory.CreateDirectory(path);
				}
				string fileName6 = path + "\\Project" + version + ".db";
				if (new FileInfo(fileName6).Exists)
				{
					return new SqliteDatabase(path + "\\Project" + version + ".db");
				}
				byte[] pReadByte6 = Resource1.ResourceManager.GetObject("Project") as byte[];
				FileInfo fileInfo6 = new FileInfo(fileName6);
				if (!fileInfo6.Exists)
				{
                    Assist.FileUtil.WriteFile(pReadByte6, fileName6);
				}
				return new SqliteDatabase(fileName6);
			}
			default:
				return null;
			}
		}

		public static SqlServerDatabase GetSqlServerDB(string dbName, ConnectData cd)
		{
			return new SqlServerDatabase(dbName, cd);
		}

		public static SqlServerDatabase GetSqlServerDB(SubjectType sType, string version, ConnectData cd)
		{
			switch (sType)
			{
			case SubjectType.Architecture:
				return new SqlServerDatabase("Architecture" + version, cd);
			case SubjectType.HVAC:
				return new SqlServerDatabase("HVAC" + version, cd);
			case SubjectType.Electric:
				return new SqlServerDatabase("Electric" + version, cd);
			case SubjectType.Plumbing:
				return new SqlServerDatabase("Plumbing" + version, cd);
			case SubjectType.System:
				return new SqlServerDatabase("System" + version, cd);
			default:
				return null;
			}
		}

		public static MySqlDatabase GetMySqlDB(string dbName, ConnectData cd)
		{
			return new MySqlDatabase(dbName, cd);
		}

		public static MySqlDatabase GetMySqlDB(SubjectType sType, string version, ConnectData cd)
		{
			switch (sType)
			{
			case SubjectType.Architecture:
				return new MySqlDatabase("Architecture" + version, cd);
			case SubjectType.HVAC:
				return new MySqlDatabase("HVAC" + version, cd);
			case SubjectType.Electric:
				return new MySqlDatabase("Electric" + version, cd);
			case SubjectType.Plumbing:
				return new MySqlDatabase("Plumbing" + version, cd);
			case SubjectType.System:
				return new MySqlDatabase("System" + version, cd);
			case SubjectType.Company:
				return new MySqlDatabase("Company" + version, cd);
			}
			return null;
		}

		public static List<FamilyLibDatabase> SearchLocalDB()
		{
			List<FamilyLibDatabase> list = new List<FamilyLibDatabase>();
			string text = Assembly.GetExecutingAssembly().Location;
			int length = text.LastIndexOf("\\");
			text = text.Substring(0, length);
			int length2 = text.LastIndexOf("\\");
			text = @"C:\Hongye\HYArch2018";
			text += "\\FamilyLib";
			DirectoryInfo directoryInfo = new DirectoryInfo(text);
			FileInfo[] files = directoryInfo.GetFiles("*.db", SearchOption.AllDirectories);
			foreach (FileInfo fileInfo in files)
			{
				list.Add(new SqliteDatabase(fileInfo.FullName));
			}
			return list;
		}

		public abstract ISubjectTable SubjectTable { get; }

		public abstract ICategoryTable CategoryTable { get; }

		public abstract ICategoryParamTable CategoryParamTable { get; }

		public abstract IFamilyTable FamilyTable { get; }

		public abstract IFamilyParamTable FamilyParamTable { get; }

		public abstract ITypeTable TypeTable { get; }

		public abstract IFamilyFileTable FamilyFileTable { get; }

		public abstract IFamilyImgTable FamilyImgTable { get; }

		public bool Export(string fileName, ref int count)
		{
			bool result;
			try
			{
				byte[] pReadByte = null;
				switch (this.SubjectTable.SubjectType)
				{
				case SubjectType.Architecture:
					pReadByte = (Resource1.ResourceManager.GetObject("Architecture") as byte[]);
					break;
				case SubjectType.HVAC:
					pReadByte = (Resource1.ResourceManager.GetObject("HVAC") as byte[]);
					break;
				case SubjectType.Electric:
					pReadByte = (Resource1.ResourceManager.GetObject("Electric") as byte[]);
					break;
				case SubjectType.Plumbing:
					pReadByte = (Resource1.ResourceManager.GetObject("Plumbing") as byte[]);
					break;
				case SubjectType.System:
					pReadByte = (Resource1.ResourceManager.GetObject("System") as byte[]);
					break;
				case SubjectType.Project:
					pReadByte = (Resource1.ResourceManager.GetObject("Project") as byte[]);
					break;
				case SubjectType.Company:
					pReadByte = (Resource1.ResourceManager.GetObject("Company") as byte[]);
					break;
				}
				FileInfo fileInfo = new FileInfo(fileName);
				if (!fileInfo.Exists)
				{
                    Assist.FileUtil.WriteFile(pReadByte, fileName);
				}
				SqliteDatabase sqliteDatabase = new SqliteDatabase(fileName);
				DlgCompare dlgCompare = new DlgCompare(sqliteDatabase, this);
				if (dlgCompare.ShowDialog() == DialogResult.OK)
				{
					if (sqliteDatabase.Import(this, dlgCompare.CategoryList, ref count))
					{
						result = true;
					}
					else
					{
						sqliteDatabase.Dispose();
						FileInfo fileInfo2 = new FileInfo(fileName);
						fileInfo2.Delete();
						result = false;
					}
				}
				else
				{
					sqliteDatabase.Dispose();
					FileInfo fileInfo3 = new FileInfo(fileName);
					fileInfo3.Delete();
					MessageBox.Show("导出被取消。");
					result = false;
				}
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public bool Import(FamilyLibDatabase newDB, List<CategoryForSelect> categoryList, ref int count)
		{
			List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
			foreach (CategoryForSelect categoryForSelect in categoryList)
			{
				KeyValuePair<int, string> item = new KeyValuePair<int, string>(categoryForSelect.FamilyList.Count, categoryForSelect.Category.Name);
				list.Add(item);
			}
			ProgressBarForm progressBarForm = new ProgressBarForm(list, "导入导出族库", true);
			progressBarForm.Show();
			bool result;
			try
			{
				foreach (CategoryForSelect categoryForSelect2 in categoryList)
				{
					try
					{
						List<DbParamDefinition> cpdl = newDB.CategoryParamTable[categoryForSelect2.Category];
						if (this.CategoryTable[categoryForSelect2.Category.Guid] == null)
						{
							this.CategoryTable.Add(categoryForSelect2.Category, cpdl);
						}
					}
					catch
					{
					}
					foreach (FamilyForSelect familyForSelect in categoryForSelect2.FamilyList)
					{
						if (familyForSelect.Selected)
						{
							try
							{
								DbFamily dbFamily = this.FamilyTable[familyForSelect.Family.Name];
								if (dbFamily != null)
								{
									DbFamily dbFamily2 = this.FamilyTable[familyForSelect.Family.Guid];
									if (dbFamily2 != null && dbFamily.Equals(dbFamily2) && dbFamily.Date < familyForSelect.Family.Date)
									{
										FamilyLibDatabase.UpdateFamilyFile(newDB, this, familyForSelect.Family);
										count++;
									}
									else if (dbFamily2 == null)
									{
										DlgReName dlgReName = new DlgReName("导入了重名的族" + dbFamily.Name + "。是否重命名后继续导入?");
										if (dlgReName.ShowDialog() == DialogResult.OK)
										{
											familyForSelect.Family.Rename(dlgReName.txtName.Text.Trim());
											FamilyLibDatabase.DownloadFamily(newDB, this, familyForSelect.Family);
											count++;
										}
									}
								}
								else
								{
									DbFamily dbFamily3 = this.FamilyTable[familyForSelect.Family.Guid];
									if (dbFamily3 != null)
									{
										if (MessageBox.Show(string.Concat(new object[]
										{
											"族\"",
											dbFamily3.Name,
											"\"在新导入的族库中名字被更改为：\"",
											familyForSelect,
											"\"。是否继续使用原族库中的族名？"
										}), "盈建科族库", MessageBoxButtons.YesNo) != DialogResult.Yes)
										{
											dbFamily3.Rename(familyForSelect.Family.Name);
											count++;
										}
									}
									else
									{
										FamilyLibDatabase.DownloadFamily(newDB, this, familyForSelect.Family);
										count++;
									}
								}
							}
							catch
							{
							}
						}
						if (!progressBarForm.ProgressStep(1) && progressBarForm.IsStoped)
						{
							progressBarForm.Dispose();
							MessageBox.Show("中断操作！");
							return false;
						}
					}
				}
				this.UpdateFactor(newDB);
				progressBarForm.Dispose();
				result = true;
			}
			catch (Exception ex)
			{
				if (progressBarForm != null)
				{
					progressBarForm.Dispose();
				}
				throw ex;
			}
			return result;
		}

		private void UpdateFactor(FamilyLibDatabase newDB)
		{
			try
			{
				DatabaseType serverDbType;
				if (this is SqliteDatabase)
				{
					serverDbType = DatabaseType.Local;
				}
				else
				{
					serverDbType = DatabaseType.Server;
				}
				DatabaseType localDbType;
				if (newDB is SqliteDatabase)
				{
					localDbType = DatabaseType.Local;
				}
				else
				{
					localDbType = DatabaseType.Server;
				}
				UtilizationFactor.DataDownloadServer(localDbType, newDB.ConnStr, serverDbType, this.ConnStr);
			}
			catch
			{
			}
		}

		public static void UpdateFamilyFile(FamilyLibDatabase newDB, FamilyLibDatabase tagDB, DbFamily family)
		{
			try
			{
				FileInfo fi = newDB.FamilyFileTable[family];
				tagDB.FamilyFileTable.Replace(family, fi);
				tagDB.FamilyTable.Update(family);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private static void downloadCategory(DbCategory cate, FamilyLibDatabase tagDb)
		{
			FamilyLibDatabase database = cate.Database;
			if (tagDb.CategoryTable[cate.Guid] == null)
			{
				tagDb.CategoryTable.Add(cate, cate.GetParamDefinitionList());
			}
			if (cate.ParentId != database.SubjectTable.AllSubject()[0].Guid)
			{
				FamilyLibDatabase.downloadCategory(database.CategoryTable[cate.ParentId], tagDb);
			}
		}

		public static void DownloadFamily(FamilyLibDatabase newDB, FamilyLibDatabase tagDB, DbFamily family)
		{
			FileInfo fileInfo = newDB.FamilyFileTable[family];
			Image largeImg = newDB.FamilyImgTable.GetLargeImg(family);
			Image twoDImg = newDB.FamilyImgTable.Get2DImg(family);
			DbTransaction dbTransaction = null;
			try
			{
				List<DbParamDefinition> pdList = newDB.FamilyParamTable[family];
				DataTable dataTable = newDB.TypeTable.DataTable(family);
				DbCategory dbCategory = newDB.CategoryTable[family.ParentId];
				FamilyLibDatabase.downloadCategory(dbCategory, tagDB);
				dbTransaction = tagDB.StartTransaction();
				tagDB.FamilyTable.Add(dbCategory, family, dbTransaction);
				tagDB.FamilyFileTable.Add(family, fileInfo, dbTransaction);
				tagDB.FamilyImgTable.Add(family, largeImg, twoDImg, dbTransaction);
				tagDB.FamilyParamTable.Add(family, pdList, dbTransaction);
				tagDB.TypeTable.Create(family, dataTable, dbTransaction);
				tagDB.TypeTable.Add(dataTable, family, dbTransaction);
				tagDB.CommitTransaction(dbTransaction);
			}
			catch (Exception ex)
			{
				tagDB.RollbackTransaction(dbTransaction);
				throw ex;
			}
			finally
			{
				fileInfo.Delete();
				dbTransaction.Dispose();
			}
		}

		public void Compare(FamilyLibDatabase newDb, TreeView tv)
		{
			tv.Nodes.Clear();
			List<DbCategory> all = newDb.CategoryTable.GetAll();
			DbSubject dbSubject = newDb.SubjectTable.AllSubject()[0];
			TreeNode treeNode = new TreeNode(dbSubject.ToString());
			treeNode.Name = dbSubject.Name;
			treeNode.Tag = dbSubject;
			FamilyLibDatabase.SetCategoryNode(treeNode, all, this, newDb);
			tv.Nodes.Add(treeNode);
			tv.ExpandAll();
		}

		public static string GetDefDbName(SubjectType sType, string version)
		{
			string result;
			switch (sType)
			{
			case SubjectType.Architecture:
				result = "Architecture" + version + ".db";
				break;
			case SubjectType.HVAC:
				result = "HVAC" + version + ".db";
				break;
			case SubjectType.Electric:
				result = "Electric" + version + ".db";
				break;
			case SubjectType.Plumbing:
				result = "Plumbing" + version + ".db";
				break;
			case SubjectType.System:
				result = "System" + version + ".db";
				break;
			case SubjectType.Project:
				result = "Project" + version + ".db";
				break;
			default:
				result = "Architecture" + version + ".db";
				break;
			}
			return result;
		}

		public static void FillTree(TreeView tree, List<FamilyLibDatabase> dbs, CheckType checkType)
		{
			try
			{
				tree.Nodes.Clear();
				for (int i = 0; i < dbs.Count<FamilyLibDatabase>(); i++)
				{
					FamilyLibDatabase.FillTree(dbs[i], tree, checkType);
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		public static void FillTree(FamilyLibDatabase db, TreeView tree, CheckType checkType)
		{
			try
			{
				List<DbCategory> all = db.CategoryTable.GetAll();
				DbSubject dbSubject = db.SubjectTable.AllSubject()[0];
				TreeNode treeNode = new TreeNode(dbSubject.GetNameAndCount(checkType));
				treeNode.Name = dbSubject.Name;
				treeNode.Tag = dbSubject;
				FamilyLibDatabase.SetChildNode(treeNode, all, checkType);
				tree.Nodes.Add(treeNode);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private static void SetChildNode(TreeNode tn, List<DbCategory> cl, CheckType checkType)
		{
			foreach (DbCategory dbCategory in cl)
			{
				if (tn.Tag is DbCategory)
				{
					if (dbCategory.ParentId == ((DbCategory)tn.Tag).Guid)
					{
						TreeNode treeNode = new TreeNode(dbCategory.GetNameAndCount(checkType));
						treeNode.Name = dbCategory.Name;
						treeNode.Tag = dbCategory;
						treeNode.ImageIndex = 1;
						FamilyLibDatabase.SetChildNode(treeNode, cl, checkType);
						tn.Nodes.Add(treeNode);
					}
				}
				else if (tn.Tag is DbSubject)
				{
					DbSubject dbSubject = (DbSubject)tn.Tag;
					if (dbCategory.ParentId == dbSubject.Guid)
					{
						TreeNode treeNode2 = new TreeNode(dbCategory.GetNameAndCount(checkType));
						treeNode2.Name = dbCategory.Name;
						treeNode2.Tag = dbCategory;
						treeNode2.ImageIndex = 1;
						FamilyLibDatabase.SetChildNode(treeNode2, cl, checkType);
						tn.Nodes.Add(treeNode2);
					}
				}
			}
		}

		private static void SetCategoryNode(TreeNode tn, List<DbCategory> cl, FamilyLibDatabase oldDb, FamilyLibDatabase newDb)
		{
			foreach (DbCategory dbCategory in cl)
			{
				if (tn.Tag is CategoryForSelect)
				{
					if (dbCategory.ParentId == ((CategoryForSelect)tn.Tag).Category.Guid)
					{
						List<DbFamily> second = oldDb.FamilyTable[dbCategory, CheckType.All];
						List<DbFamily> first = newDb.FamilyTable[dbCategory, CheckType.All];
						List<DbFamily> list = first.Except(second).ToList<DbFamily>();
						CategoryForSelect categoryForSelect = new CategoryForSelect(dbCategory);
						foreach (DbFamily family in list)
						{
							FamilyForSelect familyForSelect = new FamilyForSelect(family);
							familyForSelect.Category = categoryForSelect;
							categoryForSelect.FamilyList.Add(familyForSelect);
						}
						TreeNode treeNode = new TreeNode(dbCategory.ToString());
						treeNode.Name = dbCategory.Name;
						treeNode.Tag = categoryForSelect;
						FamilyLibDatabase.SetCategoryNode(treeNode, cl, oldDb, newDb);
						tn.Nodes.Add(treeNode);
					}
				}
				else if (tn.Tag is DbSubject)
				{
					DbSubject dbSubject = (DbSubject)tn.Tag;
					if (dbCategory.ParentId == dbSubject.Guid)
					{
						List<DbFamily> second2 = oldDb.FamilyTable[dbCategory, CheckType.All];
						List<DbFamily> first2 = newDb.FamilyTable[dbCategory, CheckType.All];
						List<DbFamily> list2 = first2.Except(second2).ToList<DbFamily>();
						CategoryForSelect categoryForSelect2 = new CategoryForSelect(dbCategory);
						foreach (DbFamily family2 in list2)
						{
							FamilyForSelect familyForSelect2 = new FamilyForSelect(family2);
							familyForSelect2.Category = categoryForSelect2;
							categoryForSelect2.FamilyList.Add(familyForSelect2);
						}
						TreeNode treeNode2 = new TreeNode(dbCategory.ToString());
						treeNode2.Name = dbCategory.Name;
						treeNode2.Tag = categoryForSelect2;
						FamilyLibDatabase.SetCategoryNode(treeNode2, cl, oldDb, newDb);
						tn.Nodes.Add(treeNode2);
					}
				}
			}
		}

		public override string ToString()
		{
			DbSubject dbSubject = this.SubjectTable.AllSubject()[0];
			return dbSubject.Name;
		}

		public const string dbFolder = "FamilyLib";

		public const string ArchDbFileName = "Architecture";

		public const string HvacDbFileName = "HVAC";

		public const string ElectricDbFileName = "Electric";

		public const string PlumbingDbFileName = "Plumbing";

		public const string SystemDbFileName = "System";

		public const string ProjectDbFileName = "Project";

		public const string CompanyDbFileName = "Company";

		public const string MyCollectDbFileName = "MyCollect";

		public const string dbExName = ".db";

		protected string mName = "";
	}
}
